package cn.itcast.bos.service.base.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CourierRepository;
import cn.itcast.bos.dao.base.FixedAreaRepository;
import cn.itcast.bos.dao.base.TakeTimeRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.base.FixedAreaService;

/**
 * 定区的业务实现类
 * 
 * @author 王文杰
 * @version 1.0, 2017-9-9 21:40:58
 */
@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService {

	/** 注入dao */
	@Autowired
	private FixedAreaRepository fixedAreaRepository;
	
	/**
	 * @see cn.itcast.bos.service.base.FixedAreaService#add(FixedArea)
	 */
	@Override
	public void add(FixedArea model) {
		
		fixedAreaRepository.save(model);
	}

	/**
	 * @see cn.itcast.bos.service.base.FixedAreaService#findPageData(Specification, Pageable)
	 */
	@Override
	public Page<FixedArea> findPageData(Specification<FixedArea> specification, Pageable pageable) {
		
		return fixedAreaRepository.findAll(specification, pageable);
	}

	/** 注入快递员dao */
	@Autowired
	private CourierRepository courierRepository;
	/** 注入取派时间dao */
	@Autowired
	private TakeTimeRepository takeTimeRepository;
	
	/**
	 * @see cn.itcast.bos.service.base.FixedAreaService#associationCourierToFixedArea(FixedArea, Integer, Integer)
	 */
	@Override
	public void associationCourierToFixedArea(FixedArea model, Integer courierId, Integer takeTimeId) {
		// 通过传过来的fixedArea 找到对应的fixedArea
		FixedArea fixedArea = fixedAreaRepository.findOne(model.getId());
		// 通过courierID找到对应的courier
		System.out.println(courierId);
		Courier courier = courierRepository.findOne(courierId);
		// 通过taketimeid 找到对应的取派时间对象
		TakeTime takeTime = takeTimeRepository.findOne(takeTimeId);
		System.out.println(courier);
		// 将快递员关联到定区
		fixedArea.getCouriers().add(courier);
		// 将取派时间关联到快递员
		courier.setTakeTime(takeTime);
	}

}
